package com.hruiworks.campusgroup.quartz.job;

import com.alibaba.fastjson2.JSON;
import com.hruiworks.campusgroup.common.SseBaseResponse;
import com.hruiworks.campusgroup.enums.SuccessCode;
import com.hruiworks.campusgroup.manager.entity.GroupFullInfo;
import com.hruiworks.campusgroup.pojo.entity.Group;
import com.hruiworks.campusgroup.pojo.entity.User;
import com.hruiworks.campusgroup.service.SseService;
import com.hruiworks.campusgroup.utils.SpringContextUtils;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;

import static com.hruiworks.campusgroup.enums.SseMessageType.GROUP_START_NOTICE;
import static com.hruiworks.campusgroup.quartz.constants.JobDataMapKey.GROUP_MESSAGE;
import static com.hruiworks.campusgroup.quartz.constants.JobDataMapKey.USER_MESSAGE;

public class GroupStartNoticeJob implements Job {

    @Override
    public void execute(JobExecutionContext jobExecutionContext) {
        SseService sseService = SpringContextUtils.getBean(SseService.class);
        JobDataMap jobDataMap = jobExecutionContext.getJobDetail().getJobDataMap();
        String groupMessage = jobDataMap.getString(GROUP_MESSAGE);
        GroupFullInfo groupFullInfo = JSON.parseObject(groupMessage, GroupFullInfo.class);
        SseBaseResponse<GroupFullInfo> groupSseBaseResponse = new SseBaseResponse<>(groupFullInfo, SuccessCode.COMMON_SUCCESS, GROUP_START_NOTICE.getMessageType());
        String jsonString = JSON.toJSONString(groupSseBaseResponse);
        String userMessage = jobDataMap.getString(USER_MESSAGE);
        User user = JSON.parseObject(userMessage, User.class);
        sseService.sendMessage(user, jsonString);
    }
}
